<?php

namespace front\modules\app\controllers;

use common\helpers\ReturnHelper;
use front\controllers\RestBaseController;
use order\services\OrderUserService;
use users\services\RegionService;
use users\services\SmsService;
use users\services\UserService;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

/**
 * UserController implements the CRUD actions for UserModel model.
 */
class UserController extends RestBaseController
{

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'login' => ['post'],
                    'send' => ['post'],
                    'index' => ['post'],
                    'update' => ['post'],
                    'login-bind' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 登陆手机号码，验证码
     * @param mobile
     * @param password
     * @return bool
     */
    public function actionLogin()
    {
        $data = \yii::$app->request->post();
        if(!isset($data['mobile'])){
            return ReturnHelper::error_json("手机号码不能为空");
        }
        $register = UserService::registerApp($data['mobile']);
        return Json::encode($register);
    }


    /**
     * 授权登陆
     * @param mobile
     * @param password
     * @return bool
     */
    public function actionLoginBind()
    {
        $data = \yii::$app->request->post();
        if(!isset($data['mobile'])){
            return ReturnHelper::error_json('手机号码不能为空');
        }
        if(!isset($data['openid'])){
            return ReturnHelper::error_json('openid不能为空');
        }
        if(!isset($data['unionid'])){
            return ReturnHelper::error_json('unionid不能为空');
        }
        $param = [
            'mobile' => $data['mobile'],
            'openid' => $data['openid'],
            'unionid' => $data['unionid']
        ];
        $register = UserService::registerCustomer($param);
        if($register['code'] == 0){
            return ReturnHelper::success_json('success', $register['result']);
        }
        return ReturnHelper::error_json($register['message']);
    }


    /**
     *  发送验证码
     * @param $mobile
     * @return bool|string
     */
    public function actionSend()
    {
        $mobile = \yii::$app->request->post('mobile');
        $valcode = SmsService::sendSMS($mobile);
        if(!$valcode){
            return ReturnHelper::error_json('验证码发送失败');
        }
        return ReturnHelper::success_json('success',$valcode);
    }



    /**
     * 获取用户信息
     */
    function actionIndex(){
        $data = \yii::$app->request->post();
        $info = UserService::getUserInfo($data['user_id'], 1);
        $userInfo['user'] = ArrayHelper::toArray($info['result']);
        $data['getByWeb'] = 1;
        $data['default'] = 1;
        $address = OrderUserService::search($data);
        $userInfo['address']['detail'] = '';
        if($address['code'] == 0){
            $address = ArrayHelper::toArray($address['result']);
            if(!empty($address)) {
                $address = $address[0];
                $district = RegionService::getAddress($address['district']);
                $userInfo['address']['detail'] = $district . $address['address'];
                $userInfo['address']['id'] = $address['id'];
            }
        }else{
            $userInfo['address'] = [];
        }
        return ReturnHelper::error_json('success', $userInfo);
    }


    /**
     *  user update info
     *
     * @return array
     */
    public function actionUpdate()
    {
        $param = \yii::$app->request->post();
        $param['getByWeb'] = 1;
        if(isset($param['email'])){
            $param['email'] = strtolower($param['email']);
        }
        $param['isApp'] = 1;
        $info =  UserService::updateUserInfo($param);
        \yii::info($info);
        if($info['code'] == 0){
            return ReturnHelper::success_json('success');
        } else {
            return ReturnHelper::error_json($info['message']);
        }
    }

    public function actionLoginbyopenid()
    {
        $param = \yii::$app->request->post();
        if(isset($param['openid'])){
            $res = UserService::loginByOpenId($param['openid']);
            if($res['code'] == 0){
                return ReturnHelper::success_json('success', $res['result']);
            }
            return ReturnHelper::error_json($res['message']);
        }
        return ReturnHelper::error_json('openid is required!');
    }

}
